package com.IClass.App;


import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;



import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.IClass.IClassApi;
import com.IClass.Server.AppJsonDefine.GenJavaCode.appshowuser;
import com.IClass.Server.AppJsonDefine.GenJavaCode.homeblogcomment;
import com.IClass.Server.AppJsonDefine.GenJavaCode.hothomeblog;


import android.app.Application;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;


public class IClass extends Application {
    
	public static IClassApi classapi = new IClassApi();
	
	public static HashMap<String , appshowuser> appshowusermap = new HashMap<String , appshowuser>();
	public static HashMap<String , hothomeblog> hotblogs = new HashMap<String , hothomeblog>();
	public static HashMap<String , List<homeblogcomment>> blogcomments = new HashMap<String , List<homeblogcomment>>();
	public static HashMap<String , String> pictureresult = new HashMap<String , String>();
	
	public static String userid = "0";
	
	public static void GetHotBlogsData(String strResult)
	{
		JSONArray res;
		try {
			res = new JSONArray(strResult);
			
			for(int i = 0; i < res.length();i++)
			{
				JSONObject jsonobj = res.getJSONObject(i);
				
				hothomeblog hot = new hothomeblog();
				hot.uid =  jsonobj.getString("uid");
				hot.username =  jsonobj.getString("username");
				hot.avatarurl =  jsonobj.getString("avatarurl");
				hot.dateline = jsonobj.getString("dateline");
				hot.body_data = jsonobj.getString("body_data");
				hot.body_template = jsonobj.getString("body_template");
				hot.image_1 = jsonobj.getString("image_1");
				hot.title_data = jsonobj.getString("title_data");
				hot.title_template = jsonobj.getString("title_template");

				IClass.getHotblogs().put(hot.uid, hot);
			}
		} catch (JSONException e) {
			// FIXME Auto-generated catch block
			e.printStackTrace();
		}

		
	}
	
	public static void GetPictureResutlData(String strResult)
	{
		int start = 0;
		int ss = strResult.indexOf("{",start);
		int ee = strResult.indexOf("}",start);
		
		while(ss != -1 && ee != -1 && strResult.length() != ss && strResult.length() != ee)
		{
			String str = strResult.substring(ss,ee + 1);
			
			try {
				JSONObject jsonobj = new JSONObject(str);
				
				

				
					IClass.getPictureresult().put(jsonobj.getString("picpath"), jsonobj.getString("picreslutpath"));
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				start = ee + 1;
				ss = strResult.indexOf("{",start);
				ee = strResult.indexOf("}",start);
		}
	}
	public static String getUserid() {
		
		   
		return userid;
	}

	public static void setUserid(String userid1) {

        
		userid = userid1;
	}
	
	
	public static HashMap<String, String> getPictureresult() {
		return pictureresult;
	}



	public static void setPictureresult(HashMap<String, String> pictureresult) {
		IClass.pictureresult = pictureresult;
	}
	public static long getGetHotBlogstime = 0;
	public static long getGetGetHotBlogstime() {
		return getGetHotBlogstime;
	}



	public static void setGetGetHotBlogstime(long getGetHotBlogstime) {
		IClass.getGetHotBlogstime = getGetHotBlogstime;
	}



	public static HashMap<String, List<homeblogcomment>> getBlogcomments() {
		return blogcomments;
	}



	public static HashMap<String, hothomeblog> getHotblogs() {
		return hotblogs;
	}

	public static void setHotblogs(HashMap<String, hothomeblog> hotblogs) {
		IClass.hotblogs = hotblogs;
	}

	public static HashMap<String, appshowuser> getAppshowusermap() {
		return appshowusermap;
	}
	
	public static void setAppshowusermap(HashMap<String, appshowuser> appshowusermap) {
		IClass.appshowusermap = appshowusermap;
	}   
	
	public static List<Map<String, Object>> getappshowlist()
	{
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		
		for(Map.Entry<String, appshowuser> m :appshowusermap.entrySet())
		{
			Map<String, Object> map = new HashMap<String, Object>();
	        map.put("username", m.getValue().username);
	        map.put("userfeed", m.getValue().userfeed);
	        map.put("userimage", IClassApi.SERVER_HOST + "uc_server/"+ m.getValue().avatarurl);
	        list.add(map);
		}
		
		return list;
		
		
	}
	
	public static List<Map<String, Object>> gethotblogslist()
	{
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		
		for(Map.Entry<String, hothomeblog> m :hotblogs.entrySet())
		{
			Map<String, Object> map = new HashMap<String, Object>();
	        map.put("username", m.getValue().username);
	        
	        
			try {
				
				// 日志内容
				JSONObject body_data_json = new JSONObject(m.getValue().body_data);
				// 日志内容
				map.put("summary", body_data_json.get("summary"));
				// 日志标题包含链接
				
				//<a href="home.php?mod=space&uid=1&do=blog&id=5">?????????</a> 取HTML内容
			    String subject = (String) body_data_json.get("subject");
			    subject = subject.substring(subject.indexOf(">") + 1, subject.lastIndexOf("<"));
			    
			    
				map.put("subject", subject);
				// 日志 id
				JSONObject title_data_json = new JSONObject(m.getValue().title_data);
				map.put("hash_data", title_data_json.get("hash_data"));
				
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} 
	        
	        
	        map.put("body_template", m.getValue().body_template);
	        map.put("image_1", m.getValue().image_1);
	        
	        map.put("title_template", m.getValue().title_template);
	        map.put("userimage", IClassApi.SERVER_HOST + "uc_server/"+ m.getValue().avatarurl);
	        list.add(map);
		}
		
		return list;
		
		
	}
	
	public static Map<String,Map<String, Object>> gethotblogsmap()
	{
		Map<String,Map<String, Object>> blogsmap = new HashMap<String,Map<String, Object>>();
		
		for(Map.Entry<String, hothomeblog> m :hotblogs.entrySet())
		{
			Map<String, Object> map = new HashMap<String, Object>();
	        map.put("username", m.getValue().username);
	        
	        
			try {
				
				// 日志内容
				JSONObject body_data_json = new JSONObject(m.getValue().body_data);
				// 日志内容
				map.put("summary", body_data_json.get("summary"));
				// 日志标题包含链接
				
				//<a href="home.php?mod=space&uid=1&do=blog&id=5">?????????</a> 取HTML内容
			    String subject = (String) body_data_json.get("subject");
			    subject = subject.substring(subject.indexOf(">") + 1, subject.lastIndexOf("<"));
			    
			    
				map.put("subject", subject);
				// 日志 id
				JSONObject title_data_json = new JSONObject(m.getValue().title_data);
				map.put("hash_data", title_data_json.get("hash_data"));
				
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} 
	        
	        
	        map.put("body_template", m.getValue().body_template);
	        map.put("image_1", m.getValue().image_1);
	        
	        map.put("title_template", m.getValue().title_template);
	        map.put("userimage", IClassApi.SERVER_HOST + "uc_server/"+ m.getValue().avatarurl);
	        
	        String blogid = (String)map.get("hash_data");
			blogid = blogid.substring(blogid.indexOf("blogid") + 6);
			
	        blogsmap.put(blogid, map);
		}
		
		return blogsmap;
		
		
	}
	public static String getFarTime(String timedata)
	{
		int t1 = Integer.valueOf(timedata);
		int t2 = (int) (System.currentTimeMillis() / 1000);
		
		Date dif = new Date(t2 - t1);
		
		SimpleDateFormat f1 = new SimpleDateFormat("dd:hh:mm 天"); 
		SimpleDateFormat f2 = new SimpleDateFormat("hh:mm 时"); 
		SimpleDateFormat f3 = new SimpleDateFormat("mm 分"); 
		if(dif.getDay() > 0)
			return f1.format(dif);
		else if(dif.getHours() > 0)
			return f2.format(dif);
		else if(dif.getMinutes() > 0)
			return f3.format(dif);
		else
			return "0分";
	}
}